package com.app.buss.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.buss.R;
import com.app.buss.adapter.ImageAdapter;
import com.app.buss.adapter.NewsAdapter;
import com.app.buss.base.BaseFragment;
import com.app.buss.bean.ImageDataBean;
import com.app.buss.bean.NewsBean;
import com.app.buss.mvp.presenter.HomePresenter;
import com.app.buss.mvp.presenter.MainPresenter;
import com.app.buss.mvp.view.IMainView;
import com.app.buss.ui.KLineDetailActivity;
import com.app.buss.ui.Top20Activity;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HomeFragment extends BaseFragment<HomePresenter> implements IMainView {

    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.rv_news)
    RecyclerView rvNews;
    private NewsAdapter adapter;
    private List<NewsBean.ArticlesBean> list = new ArrayList<>();

    @Override
    protected HomePresenter createPresenter() {
        return new HomePresenter(this);
    }


    @Override
    public void initView(View rootView) {
        super.initView(rootView);
        initToolBar(rootView,"Rp Option");
        useBanner();
        initNews();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_home;
    }

    private void initNews() {
        adapter = new NewsAdapter(list,getActivity());
        rvNews.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvNews.setAdapter(adapter);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        banner.addBannerLifecycleObserver(this);
        mvpPresenter.getNews(getActivity());
    }

    private void useBanner() {
        List<ImageDataBean> dates = new ArrayList<>();
        dates.add(new ImageDataBean(R.drawable.banner1));
        dates.add(new ImageDataBean(R.drawable.banner2));
        dates.add(new ImageDataBean(R.drawable.banner3));
        banner.addBannerLifecycleObserver(this)//添加生命周期观察者
                .setAdapter(new ImageAdapter(dates, getActivity()))
                .setIndicator(new CircleIndicator(getActivity()));
        //更多使用方法仔细阅读文档，或者查看demo
    }

    @Override
    public void onStart() {
        super.onStart();
        //开始轮播
        banner.start();
    }

    @Override
    public void onStop() {
        super.onStop();
        //停止轮播
        banner.stop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //销毁
        banner.destroy();
    }

    @OnClick({R.id.img_top_20,R.id.ll_bt,R.id.ll_et,R.id.ll_ht})
    public void onViewClicked(View v) {
        switch (v.getId()){
            case R.id.img_top_20:
                startActivity(new Intent(getActivity(), Top20Activity.class));
                break;
            case R.id.ll_bt:
                startActivity(new Intent(getActivity(), KLineDetailActivity.class)
                        .putExtra("name","btcusdt")
                .putExtra("title","BTC/USDT"));
                break;
            case R.id.ll_et:
                startActivity(new Intent(getActivity(), KLineDetailActivity.class).putExtra("name","ethusdt")
                        .putExtra("title","ETH/USDT"));
                break;
            case R.id.ll_ht:
                startActivity(new Intent(getActivity(), KLineDetailActivity.class).putExtra("name","htusdt")
                        .putExtra("title","HT/USDT"));
                break;

        }

    }

    @Override
    public void showLoading(String msg) {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void getDataSuccess(Object model) {
        if(model instanceof NewsBean){
            NewsBean bean = (NewsBean) model;
            list.clear();
            list.addAll(bean.getArticles());
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void getDataFail(String msg) {
        Toast.makeText(getActivity(),msg,Toast.LENGTH_SHORT).show();
    }
}
